function swap(array, idx1, idx2) {
	var temp = array[idx1];
	array[idx1] = array[idx2];
	array[idx2] = temp;
}

var RuleTable = {
	conditions: new Array(),
	routeIn: '$from',
	onUpdate: undefined
};

RuleTable.synchronize = function() {
	$('.condition').each(function (idx, value) {
			RuleTable.conditions[idx] = value.value;
	});
	
	RuleTable.routeIn = $('#route_in').val();
}

RuleTable.update = function() {
	RuleTable.onUpdate();
}

RuleTable.toHtml = function() {
	var html = '';
	html += begin_table();
	
	for (idx in RuleTable.conditions) {
		html += table_addCondition(RuleTable.conditions[idx], idx*1);
	}
	
	html += end_table();
	return html;
}

RuleTable.upCondition = function(idx) {
	if (idx > 0) {
		RuleTable.synchronize();
		swap(RuleTable.conditions, idx -1, idx);
		RuleTable.update();
	}
}

RuleTable.downCondition = function(idx) {
	if (idx < RuleTable.conditions.length -1) {
		RuleTable.synchronize();
		swap(RuleTable.conditions, idx +1, idx);
		RuleTable.update();
	}
}

RuleTable.addCondition = function(idx) {
	RuleTable.synchronize();
	RuleTable.conditions.splice(idx, 0, '/.*/');
	RuleTable.update();
}

RuleTable.deleteCondition = function(idx) {
	RuleTable.synchronize();
	RuleTable.conditions.splice(idx, 1);
	RuleTable.update();
}

function begin_table() {
	var html = '';
	
	html += '<table class="rule-table table-striped" cellspacing="0" cellpadding="0">';
	
	html += '<!-- BLOCK MAIL -->';
	html += '<tr class="block_mail"><td class="block_img"><img src="/graph/mail_block.png"/></td>';
	html += '<td><div>';
	html += i18n.mail_block;
	html += '<a href="javascript: RuleTable.addCondition(0);">'+i18n.addcondition+'</a>';
	html += '</div></td></tr>';
	
	return html;
}

function table_addCondition(condition, idx) {
	var html = '';
	html += '<!-- BLOCK IF -->';
	html += '<tr class="block_if"><td class="block_img"><img src="/graph/if_block.png" /></td>';
	html += '<td>';
	html += '<div class="form-inline">';
	html += '	? is <textarea class="condition">'+condition+'</textarea>';
	
	if (idx == 0) {
		html += '<span class="help-inline">'+i18n.condition_help+'</span>';	
	}
	
	html += '<br/><br/><a href="javascript: RuleTable.addCondition('+(idx+1)+');">'+i18n.addcondition+'</a>, ';
	html += '<a href="javascript: RuleTable.deleteCondition('+idx+');">'+i18n.deletecondition+'</a>, ';
	html += '<a href="javascript: RuleTable.upCondition('+idx+');">'+i18n.upcondition+'</a>, ';
	html += '<a href="javascript: RuleTable.downCondition('+idx+');">'+i18n.downcondition+'</a>';
	html += '</div>';
	html += '</td></tr>';
	
	return html;
}

function end_table() {
	var html = '';
	
	html += '<!-- BLOCK ROUTE -->';
	html += '<tr class="block_route"><td class="block_img"><img src="/graph/route_block.png" /></td>';
	html += '<td><div>';
	html += '<!-- ROUTE TABLE -->';
	html += '<p class="form-inline">';
	html += "? is <input id=\"route_in\" name=\"route_in\" type=\"text\" value=\""+RuleTable.routeIn+"\" /><span class=\"help-inline\">"+i18n.rule_in_help+'</span>';
	html += '</p>';
	html += '<h3>' + i18n.lookuptable + '</h3>';
	html += '<p id="routeTable"></p>';
	html += '<!-- Controls -->';
	html += '<p class="form-inline">';
	html += '<input id="id" type="text" class="input-small" placeholder="id">&nbsp;';
	html += '<input id="jabber" type="text" class="input-small" placeholder="@jabber">&nbsp;';
	html += '<button id="addRoute" class="btn">' + i18n.addroute + '</button>';
	html += '</p>';
	html += '</div></td></tr>';
	
	html += '</table>';
	return html;
}
